Imports System.Data
Imports System.Data.SqlClient
Imports DevExpress.XtraExport
Imports System.Text
Imports DevExpress.XtraGrid.Export
Imports System.Threading
Imports System.Globalization
Imports DevExpress.XtraEditors.Controls

Public Class ManualSelect

    Dim WithEvents oBinding As Binding

    Private Sub MyItems_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        LoadIcon(Me)
        Me.lblItemCode.Text = ""

        RefreshGrid(0)
        '  refreshTreeListPeopleCategory()
        LoadCategory()

    End Sub

    Private Sub LoadCategory()
        'Load Category
        Dim str As String = "SELECT * FROM Category_Item"
        Dim cmd As New SqlCommand(str, gCnnPCE)
        Dim da As New SqlDataAdapter(cmd)
        Dim ds As New DataSet
        Me.cboCategory.Properties.Columns.Clear()
        cboCategory.Properties.DataSource = Nothing
        da.Fill(ds, "Category")

        cboCategory.Properties.DataSource = ds.Tables("Category")
        cboCategory.Properties.DisplayMember = "CategoryName"
        cboCategory.Properties.ValueMember = "CategoryID"

        Dim CollCategory As DevExpress.XtraEditors.Controls.LookUpColumnInfoCollection = cboCategory.Properties.Columns
        CollCategory.Add(New LookUpColumnInfo("CategoryName", "Category", 0))
        CollCategory.Add(New LookUpColumnInfo("CategoryID", "CategoryID", 0, DevExpress.Utils.FormatType.None, 0, False, DevExpress.Utils.HorzAlignment.Center))

    End Sub

    Private Sub refreshTreeListPeopleCategory()
        Dim str As String = "SELECT * FROM Category_Item"
        Dim cmd As New SqlCommand(str, gCnnPCE)
        Dim da As New SqlDataAdapter(cmd)
        Dim ds As New DataSet
        da.Fill(ds, "Category")
        Dim i As Integer
        'For i = 0 To ds.Tables("Category").Rows.Count - 1
        '    Me.ListPeopleCategory.Items.Add(ds.Tables("Category").Rows(i).Item("CategoryName"))
        'Next

        Me.ListPeopleCategory.DataSource = ds.Tables("Category")
        Me.ListPeopleCategory.DisplayMember = "CategoryName"
        Me.ListPeopleCategory.ValueMember = "CategoryID"
        Me.ListPeopleCategory.ImageIndexMember = 1
    End Sub

    Private Sub RefreshGrid(ByVal CategoryID As Integer)
        Dim str As String
        If CategoryID = 0 Then
            str = "SELECT * FROM Items WHERE Status = 1"
        Else
            str = "SELECT * FROM Items WHERE Status = 1 AND Category = " & CategoryID
        End If

        Dim cmd As New SqlCommand(str, gCnnPCE)
        Dim da As New SqlDataAdapter(cmd)
        Dim ds As New DataSet
        da.Fill(ds, "Items")
        Me.GridControl1.DataSource = ds.Tables("Items")

        lblID.DataBindings.Clear()
        lblID.DataBindings.Add(New Binding("Text", ds.Tables("Items"), "ID", True))

        lblItemCode.DataBindings.Clear()
        lblItemCode.DataBindings.Add(New Binding("Text", ds.Tables("Items"), "ItemCode", True))

        pic.DataBindings.Clear()
        Pic.DataBindings.Add(New Binding("Image", ds.Tables("Items"), "Picture", True))

    End Sub

    Private Sub ListPeopleCategory_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ListPeopleCategory.Click

        'MsgBox(Me.ListPeopleCategory.SelectedValue, MsgBoxStyle.Information)
        RefreshGrid(Me.ListPeopleCategory.SelectedValue)

    End Sub

    Private Sub btnNew_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim frm As New ItemDetail
        frm.Action = "ADD"
        frm.ShowDialog()
        RefreshGrid(0)
    End Sub

    Private Sub btnEdit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
        Dim frm As New ItemDetail
        frm.ID = Me.lblID.Text
        frm.Action = "EDIT"
        frm.ShowDialog()
        RefreshGrid(0)
    End Sub

    Private Sub btnSelect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSelect.Click
        ManualSelectItemCode = Me.lblItemCode.Text
        Me.Close()
    End Sub

    Private Sub GridView1_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles GridView1.DoubleClick
        ManualSelectItemCode = Me.lblItemCode.Text
        Me.Close()
    End Sub
End Class